import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const moduleA = {
  state:{
    s:"moduleA"
  },
  mutations:{},
  getters:{
    full2(state,getters,rootState){
      return state.s + rootState.num.toString()
    }
  },
  actions:{}
}

export default new Vuex.Store({
  state: {
    num:0
  },
  mutations: {
    //只能同步方法，异步操作会有不同结果
    //方法，方法内默认参数  state
    increment(state){
      state.num++
    },
    increment1(state, payload){
      state.num += payload.num
    },
    decrement(state){
      state.num--
    }
  },
  getters:{
    //计算属性
    //默认参数state
    full(state){
      return state.num.toString() + "111"
    },
    //使用getters参数。getters就是外层getters对象
    full1(state, getters){
      return getters.full + "222"
    }
  },
  actions: {
    // 异步操作
    increment2 (context) {
      // context.rootGetters
      setTimeout(()=>{
        context.commit('increment')
      }, 1000)
    }
  },
  modules: {
    a:moduleA
  }
})
